package com.my.control;

import java.io.File;
import java.io.IOException;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;

import com.my.bean.LiuYan;
import com.my.bean.Pic;
import com.my.bean.User;
import com.my.bo.ManagerBo;

public class DoBusinessAction extends BaseAction {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private ManagerBo mb;
	private File fileTest; // 接收这个上传的文件
	private String fileTestFileName; // Struts2提供的格式，在文件名后+FileName就是上传文件的名字
	private String caption;
	private String savePath;
	private List<File> upload;
	private List<String> note;
	private List<String> uploadContentType;
	private List<String> uploadFileName;
	private List<Pic> picList;

	public List<Pic> getPicList() {
		return picList;
	}

	public void setPicList(List<Pic> picList) {
		this.picList = picList;
	}

	public List<File> getUpload() {
		return upload;
	}

	public void setUpload(List<File> upload) {
		this.upload = upload;
	}

	public List<String> getNote() {
		return note;
	}

	public void setNote(List<String> note) {
		this.note = note;
	}

	public List<String> getUploadContentType() {
		return uploadContentType;
	}

	public void setUploadContentType(List<String> uploadContentType) {
		this.uploadContentType = uploadContentType;
	}

	public List<String> getUploadFileName() {
		return uploadFileName;
	}

	public void setUploadFileName(List<String> uploadFileName) {
		this.uploadFileName = uploadFileName;
	}

	public String getSavePath() {
		return savePath;
	}

	public void setSavePath(String savePath) {
		this.savePath = savePath;
	}

	public String getCaption() {
		return caption;
	}

	public void setCaption(String caption) {
		this.caption = caption;
	}

	public File getFileTest() {
		return fileTest;
	}

	public void setFileTest(File fileTest) {
		this.fileTest = fileTest;
	}

	public String getFileTestFileName() {
		return fileTestFileName;
	}

	public void setFileTestFileName(String fileTestFileName) {
		this.fileTestFileName = fileTestFileName;
	}

	public ManagerBo getMb() {
		return mb;
	}

	public void setMb(ManagerBo mb) {
		this.mb = mb;
	}

	/**
	 * 登录
	 * */
	public String login() {
		// int id = mb.checkLogin(user);
		User u = mb.checkUser(user);
		if (u != null) {
			System.out.println("用户名和密码正确，登录成功！");
			session.clear();
			session.put("user", u);
			return "main";
		} else {
			System.out.println("用户名或密码错误，登录失败！");
			return "login";
		}
	}

	/**
	 * 注销
	 * */
	public String logout() throws Exception {
		request.clear();
		session.remove("user");
		session.clear();
		return "login";
	}

	/**
	 * 多图片上传
	 * 
	 * */
	public String saveupload() throws Exception {
		List<File> files = getUpload();
		List<String> filenotes = getNote();
		List<String> fileNames = getUploadFileName();
		for (int i = 0; i < files.size(); i++) {
			String name = System.currentTimeMillis()
					+ this.getExtention(fileNames.get(i));
			String realPath = ServletActionContext.getServletContext()
					.getRealPath(getSavePath());// 实际路径
			String url = getSavePath() + "/" + name;
			File saveFile = new File(new File(realPath), name); // 在该实际路径下实例化一个文件
			if (!saveFile.getParentFile().exists()) {
				saveFile.getParentFile().mkdirs();
			}
			try {
				FileUtils.copyFile(files.get(i), saveFile);

			} catch (Exception e) {
				System.out.println("图片上传失败");
				return "error";
			}
			pic.setUrl(url);
			pic.setCaption(filenotes.get(i));
			if (mb.add(pic)) {
				System.out.println("第" + (i + 1) + "图片保存数据库成功");
			} else {
				System.out.println("第" + (i + 1) + "图片保存数据库失败");
				return "error";
			}
		}
		return "success";
	}

	/**
	 * 获取图片后缀
	 * */
	private String getExtention(String fileName) {
		int pos = fileName.lastIndexOf(".");
		return fileName.substring(pos);
	}

	/**
	 * 单图片上传
	 * */
	@Override
	public String execute() throws Exception {
		// String[] str = { ".jpg", ".jpeg", ".bmp", ".gif" };
		// 限定文件大小是2MB
		if (fileTest == null || fileTest.length() > 2097152) {
			return "error";
		}
		// for (String s : str) {
		// if (fileTestFileName.endsWith(s)) {
		String realPath = ServletActionContext.getServletContext().getRealPath(
				getSavePath());// 实际路径
		String name = System.currentTimeMillis()
				+ this.getExtention(fileTestFileName);
		File saveFile = new File(new File(realPath), name); // 在该实际路径下实例化一个文件
		@SuppressWarnings("unused")
		String url = getSavePath() + "/" + name;
		// 判断父目录是否存在
		if (!saveFile.getParentFile().exists()) {
			saveFile.getParentFile().mkdirs();
		}
		try {
			// 执行文件上传
			// FileUtils 类名 org.apache.commons.io.FileUtils;
			// 是commons-io包中的，commons-fileupload 必须依赖
			// commons-io包实现文件上次，实际上就是将一个文件转换成流文件进行读写
			FileUtils.copyFile(fileTest, saveFile);

		} catch (IOException e) {
			System.out.println("图片上传失败");
			return "error";
		}
		/*
		 * pic.setUrl(url); pic.setCaption(getCaption()); if (mb.add(pic)) {
		 * return "success"; } else { System.out.println("图片保存数据库失败"); return
		 * "error"; }
		 */
		// }
		// }
		return "success";
	}

	/**
	 * 图片一览
	 * 
	 * @throws Exception
	 * */
	public String list() throws Exception {
		picList = mb.getAllPic();
		return "list";
	}

	/**
	 * 全部留言
	 * 
	 */
	public String listLiuYan() throws Exception {
		List<LiuYan> listLiuYan = mb.getAllLiuYan();
		request.put("listLiuYan", listLiuYan);
		return "liuYan";
	}
}
